package com.xxxx;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.util.Arrays;

public class Hello05SlidingWindowTime {
    public static void main(String[] args) throws Exception {
        //获取执行环境
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        //获取数据源
        DataStreamSource<String> source = environment.socketTextStream("192.168.88.101", 18880);
        //开始进行转换,Lambda 一定要主要添加返回类型
        source.flatMap((String line, Collector<String> collector) -> {
            Arrays.stream(line.split(" ")).forEach(word -> collector.collect(word));
        }).returns(Types.STRING).map(word -> {
            return Tuple2.of(word, 1);
        }).returns(Types.TUPLE(Types.STRING, Types.INT)).keyBy(0).timeWindow(Time.seconds(10), Time.seconds(5)).sum(1).print();

        //启动流式计算
        environment.execute("Hello05SlidingWindowTime" + System.currentTimeMillis());//获取执行环境

    }
}
